/**
 * 过滤器
 */

/**
 * 分转元
 * */
export const centToYuan = (cent) => {
  let number = parseInt(cent)
  if (isNaN(number)) return cent
  return (number / 100).toFixed(2)
}
/**
 * 分
 * */
export const centToFen = (cent) => {
  let number = parseInt(cent)
  if (isNaN(number)) return cent
  return (number).toFixed(2)
}
/**
 * 分转元取整
 * */
export const centToYuanToInt = (cent) => {
  let number = parseInt(cent)
  if (isNaN(number)) return cent
  return (number / 100).toFixed(0)
}

/**
 * 格式化日期
 * formatDate(Date.now(),"yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 * formatDate(Date.now(),"yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
 * */
export const formatDate = (timestamp, format = 'yyyy-MM-dd hh:mm:ss') => {
  if (!timestamp) return ''
  let date = new Date(timestamp)
  if (!(date && !isNaN(date.getTime()))) return timestamp

  let fmt = format
  let o = {
    'M+': date.getMonth() + 1, // <!--月份-->
    'd+': date.getDate(), // <!--日-->
    'h+': date.getHours(), // <!--小时-->
    'm+': date.getMinutes(), // <!--分-->
    's+': date.getSeconds(), // <!--秒-->
    'q+': Math.floor((date.getMonth() + 3) / 3), // <!--季度-->
    'S': date.getMilliseconds() // <!--毫秒-->
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

/**
 * 类型
 * */
export const cnToimg = (articleTypeStr) => {
  let img = ''

  if (articleTypeStr === '亲子教育') {
    img = 'http://file.mimiedu.com/20170620/20/d768a9d5aa19442cb174c7e5e38616a7.png'
  } else if (articleTypeStr === '校园风采') {
    img = 'http://file.mimiedu.com/20170620/21/18ac4eced5a04123aa6f764f3fc60be7.png'
  } else if (articleTypeStr === '艺术资讯') {
    img = 'http://file.mimiedu.com/20170620/12/4ac4a1bbd4094800a601bc092bf227de.png'
  } else if (articleTypeStr === '艺术修养') {
    img = 'http://file.mimiedu.com/20170620/1/6de6a8a380f94842b729be0b239d4562.png'
  } else if (articleTypeStr === '知识难点') {
    img = 'http://file.mimiedu.com/20170620/13/f5717bd75e9948fba03a217fb05203a0.png'
  } else if (articleTypeStr === '社会实践') {
    img = 'http://file.mimiedu.com/20170620/37/853a562fe93a423c9d6ba9877010cbd6.png'
  }

  return img
}

/**
 * 默认头像
 * */
export const defaultAvatar = (url) => {
  return url || require('../assets/images/men.png')
}

/**
 * 格式化有效期
 * */
export const validDateFormat = (month) => {
  if (month === 0) return ''

  if (!month) return month

  if (month % 12 === 0) return `/${(month / 12) === 1 ? '' : month / 12}年`

  if (month === 6) return '/半年'

  return `/${month}月`
}
